package control;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

// 三种按钮(普通,单选,复选)
public class ButtonDemo extends Application { 
	private Pane root = new Pane();		// Pane中才能移动
	private Text text;
	private Button toLeft;		// 普通按钮
	private Button toRight;
	private CheckBox underlineButton;		// 复选按钮
	private CheckBox deletelineButton;
	private ToggleGroup group = new ToggleGroup();    
	private RadioButton turnRed;		// 单选按钮
	private RadioButton turnOrange;
	private RadioButton turnGreen;
	
	public static void main(String[] args) {
		Application.launch(args);
	}
	
	@Override
	public void start(Stage a) {
		
		root.setStyle("-fx-background-color: #CCE6FF ");
		
		// 演示文本
		text = new Text(220, 200, "JavaFX is cool");
		
		// 普通按钮
		toLeft = new Button("left", new Circle(5,Color.AQUA));       // null为无色
	    toLeft.setContentDisplay(ContentDisplay.TOP);
	    toLeft.setLayoutX(200);
	    toLeft.setLayoutY(350);
	    
	    toRight = new Button("right",new Circle(5,Color.AQUA));  
	    toRight.setContentDisplay(ContentDisplay.TOP);
	    toRight.setLayoutX(260);	
	    toRight.setLayoutY(350);
//	    toRight.setScaleX(2);  // 把宽度缩放到原来的两倍
	    
	    // 复选按钮
		underlineButton = new CheckBox("下划线");       
		underlineButton.setSelected(true);                 // 设置选中状态
//		underlineButton.setAllowIndeterminate(true);		 // 允许不确定状态
		underlineButton.setLayoutX(430);
		underlineButton.setLayoutY(150);
		
		deletelineButton = new CheckBox("删除线");
		deletelineButton.setSelected(false);
		deletelineButton.setLayoutX(430);
		deletelineButton.setLayoutY(200);
	    
		// 单选按钮
		turnRed = new RadioButton("red");   
		turnRed.setGraphic(new Circle(5, Color.RED));    // 下划线与删除线也会被赋色
		turnRed.setLayoutX(10);
		turnRed.setLayoutY(155);
		turnRed.setToggleGroup(group);                   // 成为一组按钮
		
		turnGreen = new RadioButton("green");   
		turnGreen.setGraphic(new Circle(5, Color.GREEN)); 
		turnGreen.setLayoutX(10);
		turnGreen.setLayoutY(185);
		turnGreen.setToggleGroup(group);
		
		turnOrange = new RadioButton("orange");   
		turnOrange.setGraphic(new Circle(5, Color.ORANGE));
		turnOrange.setLayoutX(10);
		turnOrange.setLayoutY(215);
		turnOrange.setToggleGroup(group);
		
		root.getChildren().addAll(text, toLeft, toRight, underlineButton, deletelineButton, 
						turnRed, turnGreen, turnOrange);
		
		Scene scene = new Scene(root, 500, 400);         // 场景大小，即主面板大小
		a.setScene(scene);
		a.setTitle("Button");
		a.show();
		
		// 事件
		toLeft.setOnMouseClicked(e -> text.setLayoutX(text.getLayoutX() - 5) );
		toRight.setOnMouseClicked(e -> text.setLayoutX(text.getLayoutX() + 5) );
		underlineButton.setOnMouseClicked(e -> setUnderline() );
		deletelineButton.setOnMouseClicked(e -> setDeleteline() );
		turnRed.setOnMouseClicked(e -> text.setFill(Color.RED) );
		turnOrange.setOnMouseClicked(e -> text.setFill(Color.ORANGE) );
		turnGreen.setOnMouseClicked(e -> text.setFill(Color.GREEN) );
	}
	
	// 设置下划线
	public void setUnderline() {
		if(underlineButton.isSelected() == true) {
			text.setUnderline(true);
		} else {
			text.setUnderline(false);
		}
	}
	
	// 设置删除线
	public void setDeleteline() {
		if(deletelineButton.isSelected() == true) {
			text.setStrikethrough(true);
		} else {
			text.setStrikethrough(false);
		}
	}
}

